﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;

using Report.Helper;
using Report.Entity;
using Report.Bussiness;
using Report.Repository;

// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace Report.Controllers
{
    /// <summary>
    /// 仓库
    /// </summary>
    [Route("api/[controller]")]
    [Authorize(Roles = "Admin")]
    public class StorageController : Controller
    {
        private StorageBLL BLL = new StorageBLL();
        private PaiGongDal paiGongDal = new PaiGongDal();
   
        /// <summary>
        /// 入库
        /// </summary>
        /// <param name="para"></param>
        /// <returns></returns>

        [HttpPost]
        [Route("In")]
        public JsonResult In([FromBody]Newtonsoft.Json.Linq.JObject para)
        {
            string barcode = para["barcode"].ToString();
            string position = para["position"].ToString();

            string uid = Request.Headers["Uid"].ToString();
            string result = BLL.In(barcode, position, uid);
            if (result.Equals("true"))
            {
                BarCode barCoDeBeen = paiGongDal.searchChanpin(barcode);
                var resulto = new
                {
                    recode = 1,
                    msg = "入库成功",
                    info = barCoDeBeen
                };
                return Json(resulto);
            }
            else {
                BarCode barCoDeBeen = null;
                return Json(new { recode = -1, msg = result,info= barCoDeBeen });
            } 
        }
        /// <summary>
        /// 查询派工单
        /// </summary>
        /// <param name="para"></param>
        /// <returns></returns>

        [HttpPost]
        [Route("PaiGong")]
        public JsonResult PaiGong([FromBody]Newtonsoft.Json.Linq.JObject para)
        {
            string paigongdan = para["paigongdan"].ToString();

            PaiGongdan paigongdanBeen = paiGongDal.SearchByDingdan(paigongdan);
            var result = new { recode = (paigongdanBeen==null?-1:1), msg = (paigongdanBeen == null ?"没有数据":"成功"),
                info = paigongdanBeen
            };

            return Json(result);
        }
        /// <summary>
        /// 出库
        /// </summary>
        /// <param name="para"></param>
        /// <returns></returns>

        [HttpPost]
        [Route("Out")]
        public JsonResult Out([FromBody]Newtonsoft.Json.Linq.JObject para)
        {
            string barcode = para["barcode"].ToString();
            string paigonganId = para["paigonganId"].ToString();

            string uid = Request.Headers["Uid"].ToString();
            PaiGongDal paiGongDal=new PaiGongDal();
            string result = paiGongDal.outKuCun(barcode, paigonganId, uid);
            if (result.Equals("true"))
            {
                BarCode barCoDeBeen= paiGongDal.searchChanpin(barcode);
                var resulto = new { recode = 1, msg = "出库成功",
                    info = barCoDeBeen
                };
                return Json(resulto);
            }
            else {
                BarCode barCoDeBeen = null;
                return Json(new { recode = -1, msg = result,info= barCoDeBeen });

            }
        }
    }
}
